<?php

class Admin_ChannelController extends Yaf_Controller_Abstract {

    private $_layout;
    private $sign = array();
    private $request = array();

    public function init() {
        $this->sign = isset($_SESSION['info']) ? $_SESSION['info'] : "";
        $this->request = $this->getRequest();
        if ($this->request->action != 'getchannel') {
            if (empty($this->sign)) {
                header("Location:/admin_login");
                exit;
            }
            if (!strstr($this->sign['permission'], strtolower($this->request->controller) )) {
                header("Content-Type:text/html;charset=utf-8");
                echo "<script>alert('无权限进行此操作，请先联系管理员！');history.back();</script>";
                exit;
            }
        }
        /* use layout */
        $this->_dispatcher = Yaf_Registry::get("dispatcher");
        $this->_layout = new LayoutPlugin('layout.phtml');
        $this->_dispatcher->registerPlugin($this->_layout);
    }

    /*
     * show admin info
     */

    public function IndexAction() {
        $Channel = new ChannelModel();
        $info_list = $Channel->lists();
        if ($keyword = $this->getRequest()->getPost('keyword', '')) {
            foreach ($info_list as $key => $value) {
                if (strstr($value, $keyword) === false) {
                    unset($info_list[$key]);
                }
            }
        }
        //=======================(分页显示)
        $info_list_array = array();
        $i = 0;
        foreach ($info_list as $key => $val) {
            $info_list_array[$i]['key'] = $key;
            $info_list_array[$i]['val'] = $val;
            $i++;
        }
        $common = new helper_common();
        $page = $this->getRequest()->getParam("page", 1);
        $info_list = $common->page($info_list_array, 10, $page);
        $page = $info_list['page'];
        $pnum = $info_list['pnum'];
        unset($info_list['page']);
        unset($info_list['pnum']);
        $this->_view->info_list = $info_list;
        $this->_view->page = $page;
        $this->_view->pnum = $pnum;
        //========================

        $this->_layout->meta_title = '渠道列表';
    }

    public function AppindexAction() {
        $Channel = new ChannelModel();
        $info_list = $Channel->app_lists();
        if ($keyword = $this->getRequest()->getPost('keyword', '')) {
            foreach ($info_list as $key => $value) {
                if (strstr($key, $keyword) === false) {
                    unset($info_list[$key]);
                }
            }
        }


        $channel_list = $Channel->lists();
        $this->_view->info_list = $channel_list;

        foreach ($info_list as $key => &$value) {
            $value = explode(',', $value);
            $tmp = array();
            foreach ($value as $val) {
                if (isset($channel_list[$val])) {
                    $tmp[] = $channel_list[$val];
                }
            }
            $tmp = implode(',', $tmp);
            $value = $tmp;
        }
        //=======================(分页显示)
        $info_list_array = array();
        $i = 0;
        foreach ($info_list as $key => $val) {
            $info_list_array[$i]['key'] = $key;
            $info_list_array[$i]['val'] = $val;
            $i++;
        }
        $common = new helper_common();
        $page = $this->getRequest()->getParam("page", 1);
        $info_list = $common->page($info_list_array, 10, $page);
        $page = $info_list['page'];
        $pnum = $info_list['pnum'];
        unset($info_list['page']);
        unset($info_list['pnum']);
        $this->_view->info_list = $info_list;
        $this->_view->page = $page;
        $this->_view->pnum = $pnum;
        //========================
        $this->_layout->meta_title = 'APP列表';
    }

    /*
     * add admin page
     *
     */

    public function AddchannelAction() {
        $Channel = new ChannelModel();
        $data = $Channel->count();
        if(isset($data)&&$data){
            $count = (max($data) + 1);
        }else{
            $count = 1;
        }
        $this->_view->count = $count;
        $this->_layout->meta_title = '添加渠道';
    }

    public function AddappAction() {
        $Channel = new ChannelModel();
        $info_list = $Channel->lists();
        $this->_view->info_list = $info_list;
        $this->_layout->meta_title = '添加APP';
    }

    /*
     * insert admin data
     *
     */

    public function InsertAction() {
        $data = array(
            'channel_name' => $this->getRequest()->getPost('channel_name', ''),
            'channel_number' => $this->getRequest()->getPost('channel_number', ''),
        );

        if (empty($data['channel_name'])) {
            $_SESSION['message'] = '渠道名称不能为空，请重新输入！';
            header("Location:addchannel");
            exit;
        }

        $Channel = new ChannelModel();
        if ($Channel->check_val($data['channel_name'])) {
            $_SESSION['message'] = '很抱歉，此名称已存在，请重新输入！';
            header("Location:addchannel");
            exit;
        }
        if ($Channel->save($data)) {
            header("Location:index");
        } else {
            $_SESSION['message'] = '很抱歉，此渠道名称已存在，请重新输入！';
            header("Location:addchannel");
        }
    }

    public function AppinsertAction() {
        $channel_name = '';
        if (isset($_POST['channel_name']) && $_POST['channel_name']) {
            $channel_name = implode(',', $_POST['channel_name']);
        }
        $data = array(
            'channel_name' => $channel_name,
            'app_number' => $this->getRequest()->getPost('app_number', ''),
        );
        if (empty($data['channel_name'])) {
            $_SESSION['message'] = '渠道名称不能为空，请重新输入！';
            header("Location:addapp");
            exit;
        }

        $Channel = new ChannelModel();
        if ($Channel->app_save($data)) {
            header("Location:appindex");
        } else {
            $_SESSION['message'] = '很抱歉，此APP名称已存在，请重新输入！';
            header("Location:addapp");
        }
    }

    /*
     * delete one admin info
     * 
     */

    public function DelAction() {
        $id = $this->getRequest()->getPost('id', '');

        if (empty($id)) {
            echo "101:删除失败！请正确选择渠道！";
            exit;
        } else {
            $Channel = new ChannelModel();
            $row = $Channel->del($id);
            if (empty($row)) {
                echo "101:删除失败，请重试！";
                exit;
            } else {
                echo "100:删除成功！";
                exit;
            }
        }
    }

    public function AppdelAction() {
        $id = $this->getRequest()->getPost('id', '');

        if (empty($id)) {
            echo "101:删除失败！请正确选择APP！";
            exit;
        } else {
            $Channel = new ChannelModel();
            $row = $Channel->app_del($id);
            if (empty($row)) {
                echo "101:删除失败，请重试！";
                exit;
            } else {
                echo "100:删除成功！";
                exit;
            }
        }
    }

    /*
     * show edit admin info 
     *
     */

    public function EditAction() {
        $Channel = new ChannelModel();
        $id = $this->getRequest()->getParam("id", 0);
        if (!empty($id)) {
            $this->_view->music_info = $Channel->get_info($id);
        } else {
            header("Location:index");
        }
        $this->_layout->meta_title = '编辑渠道';
    }

    public function AppeditAction() {
        $Channel = new ChannelModel();
        $id = $this->getRequest()->getParam("id", 0);
        if (!empty($id)) {
            $app_info = $Channel->get_app_info($id);
            $app_info[1] = explode(',', $app_info[1]);
            $this->_view->app_info = $app_info;

            $info_list = $Channel->lists();
            $this->_view->info_list = $info_list;
        } else {
            header("Location:index");
        }
        $this->_layout->meta_title = '编辑渠道';
    }

    /*
     * edit admin info 
     *
     */

    public function UpdateAction() {
        $data = array(
            'channel_name' => $this->getRequest()->getPost('channel_name', ''),
            'channel_number' => $this->getRequest()->getPost('channel_number', ''),
        );
        $channel_number = $data['channel_number'];
        if (empty($data['channel_name'])) {
            $_SESSION['message'] = '功能说明不能为空，请重新输入！';
            header("Location:edit/id/{$channel_number}");
            exit;
        }
        $Channel = new ChannelModel();
        if ($Channel->check_val($data['channel_name'])) {
            $_SESSION['message'] = '很抱歉，此名称已存在，请重新输入！';
            header("Location:edit/id/{$channel_number}");
            exit;
        }
        if ($Channel->update($data)) {
            header("Location:index");
        } else {
            $_SESSION['message'] = '很抱歉，更新失败，稍后请重试！';
            header("Location:edit/id/{$channel_number}");
        }
    }

    public function AppupdateAction() {
        $channel_name = '';
        if (isset($_POST['channel_name']) && $_POST['channel_name']) {
            $channel_name = implode(',', $_POST['channel_name']);
        }
        $data = array(
            'channel_name' => $channel_name,
            'app_number' => $this->getRequest()->getPost('app_number', ''),
        );

        $app_number = $data['app_number'];
        if (empty($data['channel_name'])) {
            $_SESSION['message'] = '渠道名称不能为空，请重新输入！';
            header("Location:appedit/id/{$app_number}");
            exit;
        }
        $Channel = new ChannelModel();
        if ($Channel->app_update($data)) {
            header("Location:appindex");
        } else {
            $_SESSION['message'] = '很抱歉，更新失败，稍后请重试！';
            header("Location:appedit/id/{$app_number}");
        }
    }

    public function GetChannelAction() {
        $key = '';
        if ($this->getRequest()->getParam("id", '')) {
            $key = $this->getRequest()->getParam("id", '');
        } else {
            $key = $this->getRequest()->getPost('id', '');
        }
        if (empty($key)) {
            echo "参数为空，APP_ID为空！";
            exit;
        } else {
            $Channel = new ChannelModel();
            $data = $Channel->get_app_info($key);
            $channel_list = $Channel->lists();

            if ($data[1]) {
                $data[1] = explode(',', $data[1]);
                $tmp = array();
                foreach ($data[1] as $val) {
                    $tmp[] = $channel_list[$val];
                }
                $data[1] = $tmp;
                echo json_encode($data[1]);
                exit;
            } else {
                echo "此APP_ID的未设置或其渠道为空！";
                exit;
            }
        }
    }

}
